package com.zhang.communication.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhang.communication.entity.ChatFriends;
import com.zhang.communication.entity.ChatMessage;
import com.zhang.communication.mapper.ChatMessageMapper;
import com.zhang.communication.service.IChatFriendsService;
import com.zhang.communication.service.IChatMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户消息表 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2024-06-07
 */
@Service
public class ChatMessageServiceImpl extends ServiceImpl<ChatMessageMapper, ChatMessage> implements IChatMessageService {
    @Autowired
    private IChatFriendsService chatListService;

    @Override
    public void sendMessage(ChatMessage message) {
        message.setSendUserId(StpUtil.getLoginIdAsLong());
        message.setCreateTime(LocalDateTime.now());
        save(message);
        ChatFriends chatList = new ChatFriends();
        chatList.setFriendId(message.getSendUserId());
        chatList.setUserId(message.getReceiveUserId());
        chatListService.createChatListItem(chatList);
    }

    @Override
    public List<ChatMessage> getChatWithAnotherMessages(String anotherId) {
        LambdaQueryWrapper<ChatMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChatMessage::getSendUserId, StpUtil.getLoginIdAsLong())
                .eq(ChatMessage::getReceiveUserId, anotherId)
                .or().eq(ChatMessage::getSendUserId, anotherId).eq(ChatMessage::getReceiveUserId, StpUtil.getLoginIdAsLong());
/*wrapper.and(i -> i.eq(Message::getSendUserId, StpUtil.getLoginIdAsLong())
                   .eq(Message::getReceiveUserId, anotherId))
       .or()
       .and(i -> i.eq(Message::getSendUserId, anotherId)
                   .eq(Message::getReceiveUserId, StpUtil.getLoginIdAsLong()));
*/
        return list(wrapper);
    }

}
